package cn.yuysaln.domain.activity.service.quota.rule.impl;

import cn.yuysaln.domain.activity.model.entity.ActivityCountEntity;
import cn.yuysaln.domain.activity.model.entity.ActivityEntity;
import cn.yuysaln.domain.activity.model.entity.ActivitySkuEntity;
import cn.yuysaln.domain.activity.model.valobj.ActivityStateVO;
import cn.yuysaln.domain.activity.service.quota.rule.AbstractActionChain;
import cn.yuysaln.types.enums.ResponseCode;
import cn.yuysaln.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Date;

@Slf4j
@Component("activity_base_action")
public class ActivityBaseActionChain extends AbstractActionChain {

    @Override
    public boolean action(ActivitySkuEntity activitySkuEntity, ActivityEntity activityEntity, ActivityCountEntity activityCountEntity) {
        log.info("活动责任链-基础信息【有效期、状态、库存(sku)】校验开始。sku:{} activityId:{}", activitySkuEntity.getSku(), activityEntity.getActivityId());
        //先查看活动是否开启
        if (!ActivityStateVO.open.equals(activityEntity.getState())){
            throw new AppException(ResponseCode.ACTIVITY_STATE_ERROR.getCode(),ResponseCode.ACTIVITY_STATE_ERROR.getInfo());
        }

        //检查活动日期状况
        Date curDate = new Date();
        if (activityEntity.getBeginDateTime().after(curDate) || activityEntity.getEndDateTime().before(curDate)){
            throw new AppException(ResponseCode.ACTIVITY_DATE_ERROR.getCode(),ResponseCode.ACTIVITY_DATE_ERROR.getInfo());
        }

        //（缓存中的）库存数量是否充足
        if (activitySkuEntity.getStockCountSurplus() <= 0){
            throw new AppException(ResponseCode.ACTIVITY_SKU_STOCK_ERROR.getCode(), ResponseCode.ACTIVITY_SKU_STOCK_ERROR.getInfo());
        }

        //交给下一个节点判断
        return next().action(activitySkuEntity, activityEntity, activityCountEntity);

    }
}
